<?php
namespace app\traits;
use think\exception\HttpResponseException;

/**
 * api数据返回格式
 * Trait ResponseTrait
 * @package app\traits
 */
trait ResponseTrait{

    /**
     * 请求成功响应
     * @param array $data
     * @param int $httpStatus
     */
    protected  function  success($data=[], int $httpStatus=200){
        throw new HttpResponseException(json($data,$httpStatus));
    }

    /**
     * 失败请求响应
     * @param $errCode
     * @param string $message
     * @param int $httpStatus
     */
        protected  function  error($errCode,$message="",int $httpStatus=400){
        $response = [
            "errCode" => $errCode,
            "message" => $message,
        ];
        throw new HttpResponseException(json($response,$httpStatus));
    }
}